package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_activity;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_activity;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_activitySearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_activityService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[活动] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_activityServiceImpl implements IMail_activityService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_activityClientService mail_activityClientService;

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_activityClientService.removeBatch(mail_activitys) ;

    }

    @Override
    public boolean update(Mail_activity mail_activity) {
        boolean bOk = false ;
        Imail_activity clientModel = this.convert2Model(mail_activity,null);
        mail_activityClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_activity) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Mail_activity> mail_activitys, int batchSize) {

        //mail_activityClientService.updateBatch(mail_activitys,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_activity clientModel = mail_activityClientService.createModel();
        clientModel.setId(id);
        mail_activityClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Mail_activity> mail_activitys, int batchSize) {

                        //mail_activityClientService.createBatch(mail_activitys,batchSize) ;

    }

    @Override
    public Mail_activity get(Integer id) {
        Imail_activity clientModel = mail_activityClientService.createModel();
        clientModel.setId(id);
        mail_activityClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_activity get(Mail_activity mail_activity) {
        Mail_activity retmail_activity = this.get(mail_activity.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_activity.getClass(), mail_activity.getClass(), false);
        copier.copy(retmail_activity, mail_activity, null);
        return mail_activity ;
    }

    @Override
    public boolean create(Mail_activity mail_activity) {
        boolean bOk = false ;
        Imail_activity clientModel = this.convert2Model(mail_activity,null);
        mail_activityClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_activity);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_activity> searchDefault(Mail_activitySearchContext context) {
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据活动查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_calendar_event(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据活动类型查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_activity_type(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据活动类型查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_previous_activity_type(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据活动类型查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_recommended_activity_type(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据便签查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_note(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_create(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_create_user(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_user(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_activity> selectByOdoo_write(Integer id){
        Mail_activitySearchContext context = new Mail_activitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity> list = new ArrayList<Mail_activity>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity> clientModelList = mail_activityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_activity convert2Model(Mail_activity domain , Imail_activity model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_activityClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getMail_template_idsDirtyFlag())
            model.setMail_template_ids(domain.getMail_template_ids());
         if(domain.getAutomatedDirtyFlag())
            model.setAutomated(domain.getAutomated());
         if(domain.getRes_nameDirtyFlag())
            model.setRes_name(domain.getRes_name());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDate_deadlineDirtyFlag())
            model.setDate_deadline(domain.getDate_deadline());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getSummaryDirtyFlag())
            model.setSummary(domain.getSummary());
         if(domain.getHas_recommended_activitiesDirtyFlag())
            model.setHas_recommended_activities(domain.getHas_recommended_activities());
         if(domain.getRes_idDirtyFlag())
            model.setRes_id(domain.getRes_id());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getFeedbackDirtyFlag())
            model.setFeedback(domain.getFeedback());
         if(domain.getRes_model_idDirtyFlag())
            model.setRes_model_id(domain.getRes_model_id());
         if(domain.getRes_modelDirtyFlag())
            model.setRes_model(domain.getRes_model());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getActivity_categoryDirtyFlag())
            model.setActivity_category(domain.getActivity_category());
         if(domain.getActivity_type_id_textDirtyFlag())
            model.setActivity_type_id_text(domain.getActivity_type_id_text());
         if(domain.getPrevious_activity_type_id_textDirtyFlag())
            model.setPrevious_activity_type_id_text(domain.getPrevious_activity_type_id_text());
         if(domain.getNote_id_textDirtyFlag())
            model.setNote_id_text(domain.getNote_id_text());
         if(domain.getCreate_user_id_textDirtyFlag())
            model.setCreate_user_id_text(domain.getCreate_user_id_text());
         if(domain.getIconDirtyFlag())
            model.setIcon(domain.getIcon());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getActivity_decorationDirtyFlag())
            model.setActivity_decoration(domain.getActivity_decoration());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getForce_nextDirtyFlag())
            model.setForce_next(domain.getForce_next());
         if(domain.getRecommended_activity_type_id_textDirtyFlag())
            model.setRecommended_activity_type_id_text(domain.getRecommended_activity_type_id_text());
         if(domain.getCalendar_event_id_textDirtyFlag())
            model.setCalendar_event_id_text(domain.getCalendar_event_id_text());
         if(domain.getRecommended_activity_type_idDirtyFlag())
            model.setRecommended_activity_type_id(domain.getRecommended_activity_type_id());
         if(domain.getActivity_type_idDirtyFlag())
            model.setActivity_type_id(domain.getActivity_type_id());
         if(domain.getCreate_user_idDirtyFlag())
            model.setCreate_user_id(domain.getCreate_user_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getNote_idDirtyFlag())
            model.setNote_id(domain.getNote_id());
         if(domain.getPrevious_activity_type_idDirtyFlag())
            model.setPrevious_activity_type_id(domain.getPrevious_activity_type_id());
         if(domain.getCalendar_event_idDirtyFlag())
            model.setCalendar_event_id(domain.getCalendar_event_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Mail_activity convert2Domain( Imail_activity model ,Mail_activity domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_activity();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getMail_template_idsDirtyFlag())
            domain.setMail_template_ids(model.getMail_template_ids());
        if(model.getAutomatedDirtyFlag())
            domain.setAutomated(model.getAutomated());
        if(model.getRes_nameDirtyFlag())
            domain.setRes_name(model.getRes_name());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDate_deadlineDirtyFlag())
            domain.setDate_deadline(model.getDate_deadline());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getSummaryDirtyFlag())
            domain.setSummary(model.getSummary());
        if(model.getHas_recommended_activitiesDirtyFlag())
            domain.setHas_recommended_activities(model.getHas_recommended_activities());
        if(model.getRes_idDirtyFlag())
            domain.setRes_id(model.getRes_id());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getFeedbackDirtyFlag())
            domain.setFeedback(model.getFeedback());
        if(model.getRes_model_idDirtyFlag())
            domain.setRes_model_id(model.getRes_model_id());
        if(model.getRes_modelDirtyFlag())
            domain.setRes_model(model.getRes_model());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getActivity_categoryDirtyFlag())
            domain.setActivity_category(model.getActivity_category());
        if(model.getActivity_type_id_textDirtyFlag())
            domain.setActivity_type_id_text(model.getActivity_type_id_text());
        if(model.getPrevious_activity_type_id_textDirtyFlag())
            domain.setPrevious_activity_type_id_text(model.getPrevious_activity_type_id_text());
        if(model.getNote_id_textDirtyFlag())
            domain.setNote_id_text(model.getNote_id_text());
        if(model.getCreate_user_id_textDirtyFlag())
            domain.setCreate_user_id_text(model.getCreate_user_id_text());
        if(model.getIconDirtyFlag())
            domain.setIcon(model.getIcon());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getActivity_decorationDirtyFlag())
            domain.setActivity_decoration(model.getActivity_decoration());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getForce_nextDirtyFlag())
            domain.setForce_next(model.getForce_next());
        if(model.getRecommended_activity_type_id_textDirtyFlag())
            domain.setRecommended_activity_type_id_text(model.getRecommended_activity_type_id_text());
        if(model.getCalendar_event_id_textDirtyFlag())
            domain.setCalendar_event_id_text(model.getCalendar_event_id_text());
        if(model.getRecommended_activity_type_idDirtyFlag())
            domain.setRecommended_activity_type_id(model.getRecommended_activity_type_id());
        if(model.getActivity_type_idDirtyFlag())
            domain.setActivity_type_id(model.getActivity_type_id());
        if(model.getCreate_user_idDirtyFlag())
            domain.setCreate_user_id(model.getCreate_user_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getNote_idDirtyFlag())
            domain.setNote_id(model.getNote_id());
        if(model.getPrevious_activity_type_idDirtyFlag())
            domain.setPrevious_activity_type_id(model.getPrevious_activity_type_id());
        if(model.getCalendar_event_idDirtyFlag())
            domain.setCalendar_event_id(model.getCalendar_event_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
